/**
 * All rights Reserved, Designed By 溪云阁
 * Copyright:    Copyright(C) 2016-2020
 */

package com.boots.copier;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import com.boots.copier.model.UserFrom;
import com.boots.copier.model.UserTo;

import net.sf.cglib.beans.BeanCopier;

/**
 * apache common的对象复制
 * @author：溪云阁
 * @date：2020年5月14日
 */

public class ObjectCopier {

    /**
     * 采用apache common的BeanUtils对象复制
     * @author 溪云阁
     * @throws InvocationTargetException
     * @throws IllegalAccessException void
     */
    public static void beanCopier() throws InvocationTargetException, IllegalAccessException {
        final UserFrom userFrom = new UserFrom();
        userFrom.setName("name");
        userFrom.setCreateDate(new Date());
        userFrom.setId(1L);
        userFrom.setAge(10);
        final UserTo userTo = new UserTo();
        final long begin = System.currentTimeMillis();
        // 一百万次
        for (int i = 0; i < 1000000; i++) {
            BeanUtils.copyProperties(userTo, userFrom);
        }
        final long end = System.currentTimeMillis();
        final long mis = end - begin;
        System.out.println("apache的BeanUtils耗时" + mis + "毫秒");
    }

    /**
     * 采用apache common的PropertyUtils对象复制
     * @author 溪云阁
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException void
     */
    public static void propertyCopier()
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        final UserFrom userFrom = new UserFrom();
        userFrom.setName("name");
        userFrom.setCreateDate(new Date());
        userFrom.setId(1L);
        userFrom.setAge(10);
        final UserTo userTo = new UserTo();
        final long begin = System.currentTimeMillis();
        // 一百万次
        for (int i = 0; i < 1000000; i++) {
            PropertyUtils.copyProperties(userTo, userFrom);
        }
        final long end = System.currentTimeMillis();
        final long mis = end - begin;
        System.out.println("apache的PropertyUtils耗时" + mis + "毫秒");
    }

    /**
     * 使用spring的beanUtils进行对象复制
     * @author 溪云阁
     * @throws IllegalAccessException
     * @throws InvocationTargetException void
     */
    public static void springBeanUtilsCopier() throws IllegalAccessException, InvocationTargetException {
        final UserFrom userFrom = new UserFrom();
        userFrom.setName("name");
        userFrom.setCreateDate(new Date());
        userFrom.setId(1L);
        userFrom.setAge(10);
        final UserTo userTo = new UserTo();
        final long begin = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            BeanUtils.copyProperties(userFrom, userTo);
        }
        final long end = System.currentTimeMillis();
        final long mis = end - begin;
        System.out.println("Spring的BeanUtils耗时" + mis + "毫秒");
    }

    /**
     * 使用spring的beanCopier进行对象复制
     * @author 溪云阁  void
     */
    public static void springBeanCopierCopier() {
        final UserFrom userFrom = new UserFrom();
        userFrom.setName("name");
        userFrom.setCreateDate(new Date());
        userFrom.setId(1L);
        userFrom.setAge(10);
        final UserTo userTo = new UserTo();
        final long begin = System.currentTimeMillis();
        final BeanCopier copier = BeanCopier.create(UserFrom.class, UserTo.class, false);
        for (int i = 0; i < 1000000; i++) {
            copier.copy(userFrom, userTo, null);
        }
        final long end = System.currentTimeMillis();
        final long mis = end - begin;
        System.out.println("Spring的BeanCopier耗时" + mis + "毫秒");
    }

    /**
     * 采用asm进行对象复制
     * @author 溪云阁  void
     */
    public static void asmBeanCopier() {
        final UserFrom userFrom = new UserFrom();
        userFrom.setName("name");
        userFrom.setCreateDate(new Date());
        userFrom.setId(1L);
        userFrom.setAge(10);
        final UserTo userTo = new UserTo();
        final long begin = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            AsmCopyUtils.copyProperties(userTo, userFrom);
        }
        final long end = System.currentTimeMillis();
        final long mis = end - begin;
        System.out.println("asm的BeanCopier耗时" + mis + "毫秒");
    }

    /**
     * 采用cglib进行对象复制
     * @author 溪云阁  void
     */
    public static void cglibBeanCopier() {
        final UserFrom userFrom = new UserFrom();
        userFrom.setName("name");
        userFrom.setCreateDate(new Date());
        userFrom.setId(1L);
        userFrom.setAge(10);
        final UserTo userTo = new UserTo();
        final long begin = System.currentTimeMillis();
        final BeanCopier copier = BeanCopier.create(UserFrom.class, UserTo.class, false);
        for (int i = 0; i < 1000000; i++) {
            copier.copy(userFrom, userTo, null);
        }
        final long end = System.currentTimeMillis();
        final long mis = end - begin;
        System.out.println("cglib的BeanCopier耗时" + mis + "毫秒");
    }

    /**
     * 普通setget的耗时
     * @author 溪云阁  void
     */
    public static void setGetCopier() {
        final UserFrom userFrom = new UserFrom();
        userFrom.setName("name");
        userFrom.setCreateDate(new Date());
        userFrom.setId(1L);
        userFrom.setAge(10);
        final UserTo userTo = new UserTo();
        final long begin = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            userTo.setName(userFrom.getName());
            userTo.setCreateDate(userFrom.getCreateDate());
            userTo.setId(userFrom.getId());
            userTo.setAge(userFrom.getAge());
        }
        final long end = System.currentTimeMillis();
        final long mis = end - begin;
        System.out.println("setget的耗时" + mis + "毫秒");
    }

    public static void main(String[] args) throws Exception {
        beanCopier();
        propertyCopier();
        springBeanUtilsCopier();
        springBeanCopierCopier();
        asmBeanCopier();
        cglibBeanCopier();
        setGetCopier();
    }

}
